<?php

namespace Lowpress\PressBundle\Extension;

use Symfony\Component\DependencyInjection\ContainerInterface;

class PreviewExtension extends \Twig_Extension {

    private $definitions;

    /**
     * Container
     *
     * @var ContainerInterface
     */
    protected $container;

    /**
     * Initialize tinymce  helper
     *
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->definitions = array();
    }

    /**
     * @return ContainerInterface
     */
    public function getContainer()
    {
        return $this->container;
    }

    public function getFilters() {
        return array(
            'preview_controller'  => new \Twig_Filter_Method($this, 'preview_controller'),
        );
    }

    public function addPressItemConfigurationId($id)
    {
      $this->definitions[] = $this->getContainer()->get($id);
    }

    public function preview_controller(\Lowpress\PressBundle\Entity\Item $item)
    {
      foreach ($this->definitions as $definition)
      {
        if ($definition->getEntityName() == get_class($item))
        {
          return $definition->getPreviewController();
        }
      }
      throw new \Exception("Can not find configuration for class ".get_class($item));
    }

    public function getName()
    {
        return 'preview_extension';
    }
}